package cn.marinda.totipotentwar.global;

import cn.marinda.totipotentwar.utils.ItemUtil;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏背包
 *
 * @author hwj
 * @date 2022/02/17
 */
public class GameItemStack extends AbstractGameItemStack{
    private Map<Player,List<ItemStack>> playerItemStack = new HashMap<>();
    private List<ItemStack> itemStackList = new ArrayList<>();

    /**
     * 游戏项目堆栈
     *
     * @param player 球员
     */
    public GameItemStack(Player player,GameData gameData,WeaponsData weaponsData)
    {
        super(player,gameData,weaponsData);
        defaultItemStack();
    }


    /**
     * 默认项堆栈
     * slot: 4中心放置武器 8存放离开游戏(粘液球) 0存放当前信息(信息)
     */
    private void defaultItemStack(){

        Inventory inv = player.getInventory();

        List<String> infoLore = new ArrayList<>();
        infoLore.add("§c玩家名字：" + gameData.getPlayer().getName());
        infoLore.add("§c当前积分：" + gameData.getIntegral());
        infoLore.add("§c当前点数：" + gameData.getPoints());
        infoLore.add("§c当前阶段：" + gameData.getPhase());
        //下界之星用作显示当前数据信息。
        //基础数据显示。
        ItemStack info = ItemUtil.createItemStack(Material.NETHER_STAR,"§b§l当前信息",infoLore);
        inv.setItem(0,info);
        //weapons 武器
        Weapons weapons = weaponsData.getWeaponsMap().get("weapons1");
        int weaponsAmount = weapons.getAmount();
        String weaponsDisplayName = weapons.getDisplayName();
        List<String> weaponsLore = weapons.getLore();
        String weaponsMaterial = weapons.getMaterial();
        ItemStack weaponsItem = ItemUtil.createItemStack(Material.getMaterial(weaponsMaterial),weaponsAmount,weaponsDisplayName,weaponsLore);
        //添加附魔属性。
        for(String enChantMent : weapons.getEnchant()){
            weaponsItem.addEnchantment(Enchantment.getByName(enChantMent),1);
        }
        inv.setItem(4,weaponsItem);
        List<String> leaveLores = new ArrayList<>();
        leaveLores.add("§6\t 点我离开游戏。");
        leaveLores.add("§c退出游戏。");
        ItemStack leave = ItemUtil.createItemStack(Material.SLIME_BALL,1,"§b§l离开游戏",leaveLores);

        inv.setItem(8,leave);
        itemStackList.add(info);
        itemStackList.add(weaponsItem);
        itemStackList.add(leave);
        playerItemStack.put(player,itemStackList);
    }

    /**
     * 根据type 区分游戏背包和原背包物品
     *
     * @return {@link List}<{@link ItemStack}>
     */
    @Override
    public List<ItemStack> getItemStack(String type) {
        if(type.equalsIgnoreCase("game")){
           return playerItemStack.get(player);
        }
        return null;
    }

    /**
     * 更换武器
     *
     * @param replaceItemStack 替换项堆栈
     */
    public void replaceWeapons(ItemStack replaceItemStack)
    {

    }
}
